package com.cal.utms.model.payment;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dto.payment.JounalEntryDto;
import com.cal.utms.dto.payment.PaymentDto;
import com.cal.utms.dto.payment.ReceiptDto;
import com.cal.utms.dto.payment.RedemptionDto;
import com.cal.utms.entity.CustomerInvestment;
import com.cal.utms.entity.CustomerRedemptionRequest;
import com.cal.utms.entity.Fund;
import com.cal.utms.entity.Journalentry;
import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.entity.Payment;
import com.cal.utms.entity.Receipt;

public class MoneyTransactionModel {

	SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);

	NewFundDao dao = new NewFundDao();

	public List<MoneyTransaction> getInvestmetTransaction(String confirmDate, int investment_id, String amount, int customer, int fund_id) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		Date date = null;
		double amou = 0;
		try {
			date = format.parse(confirmDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (amount != null) {
			amount = amount.replaceAll(",", "");
			amou = Double.parseDouble(amount);
		}
		// Debit Transaction
		Fund fund = dao.getById(fund_id);
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("D");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(BigDecimal.valueOf(amou).negate());
		creditTransaction.setTranTypeId(investment_id);
		creditTransaction.setTranType("INV");
		creditTransaction.setTranTo(customer);
		creditTransaction.setFund(fund);
		creditTransaction.setEffectDate(date);
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("CUS");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		// Credit Transaction
		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("C");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(date);
		debitTransaction.setTranAmont(BigDecimal.valueOf(amou));
		debitTransaction.setTranTypeId(investment_id);
		debitTransaction.setTranType("INV");
		debitTransaction.setFund(fund);
		debitTransaction.setTranTo(fund_id);
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType("FUN");
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;
	}

	public List<MoneyTransaction> getRedemptionTransaction(CustomerRedemptionRequest customerRedemptionRequest, RedemptionDto redemptionDto, int redemption_id) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Debit Transaction
		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("D");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(customerRedemptionRequest.getExpectedDate());
		debitTransaction.setTranAmont(customerRedemptionRequest.getTotAmount().negate());
		debitTransaction.setTranTypeId(redemption_id);
		debitTransaction.setTranType("RED");
		debitTransaction.setFund(customerRedemptionRequest.getFund());
		debitTransaction.setTranTo(customerRedemptionRequest.getFund().getId());
		debitTransaction.setCustomerType("FUN");
		debitTransaction.setStatus("");
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		// Credit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("C");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(customerRedemptionRequest.getTotAmount());
		creditTransaction.setTranTypeId(redemption_id);
		creditTransaction.setTranType("RED");
		creditTransaction.setTranTo(customerRedemptionRequest.getCustomer().getId());
		creditTransaction.setFund(customerRedemptionRequest.getFund());
		creditTransaction.setEffectDate(customerRedemptionRequest.getExpectedDate());
		creditTransaction.setStatus("");
		creditTransaction.setCustomerType("CUS");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		return moneyTransactions;
	}

	public List<MoneyTransaction> getPaymentTransaction(PaymentDto paymentDto, Payment payment) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Credit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("C");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(payment.getAmount());
		creditTransaction.setTranTypeId(payment.getId());
		creditTransaction.setTranType("PAY");
		creditTransaction.setTranTo(payment.getFund().getId());
		creditTransaction.setFund(payment.getFund());
		creditTransaction.setEffectDate(new Date());
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("FUN");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		// Credit Transaction
		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("D");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(new Date());
		debitTransaction.setTranAmont(payment.getAmount().negate());
		debitTransaction.setTranTypeId(payment.getId());
		debitTransaction.setTranType("PAY");
		debitTransaction.setFund(payment.getFund());
		debitTransaction.setTranTo(payment.getCustomerId());
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType(payment.getCustomerType());
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;
	}

	public List<MoneyTransaction> getReceiptTransaction(ReceiptDto receiptDto, Receipt receipt) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Credit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("D");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(receipt.getAmount().negate());
		creditTransaction.setTranTypeId(receipt.getId());
		creditTransaction.setTranType("REC");
		creditTransaction.setTranTo(receipt.getFund().getId());
		creditTransaction.setFund(receipt.getFund());
		creditTransaction.setEffectDate(new Date());
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("FUN");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("C");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(new Date());
		debitTransaction.setTranAmont(receipt.getAmount());
		debitTransaction.setTranTypeId(receipt.getId());
		debitTransaction.setTranType("REC");
		debitTransaction.setFund(receipt.getFund());
		debitTransaction.setTranTo(receipt.getCustomerId());
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType(receipt.getCustomerType());
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;
	}

	public List<MoneyTransaction> getTransactionReverse(CustomerInvestment customerInvestment) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Debit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("C");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(customerInvestment.getAmount());
		creditTransaction.setTranTypeId(customerInvestment.getId());
		creditTransaction.setTranType("INV");
		creditTransaction.setTranTo(customerInvestment.getCustomer().getId());
		creditTransaction.setFund(customerInvestment.getFund());
		creditTransaction.setEffectDate(new Date());
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("CUS");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		// Credit Transaction
		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("D");
		debitTransaction.setDate(new Date());
		debitTransaction.setTranAmont(customerInvestment.getAmount().negate());
		debitTransaction.setTranTypeId(customerInvestment.getId());
		debitTransaction.setTranType("INV");
		debitTransaction.setTranTo(customerInvestment.getFund().getId());
		debitTransaction.setFund(customerInvestment.getFund());
		debitTransaction.setEffectDate(new Date());
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType("FUN");
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;

	}

	public List<MoneyTransaction> getReceiptReversTranasactions(Receipt receipt) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Credit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("C");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(receipt.getAmount());
		creditTransaction.setTranTypeId(receipt.getId());
		creditTransaction.setTranType("REC");
		creditTransaction.setTranTo(receipt.getFund().getId());
		creditTransaction.setFund(receipt.getFund());
		creditTransaction.setEffectDate(new Date());
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("FUN");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("D");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(new Date());
		debitTransaction.setTranAmont(receipt.getAmount().negate());
		debitTransaction.setTranTypeId(receipt.getId());
		debitTransaction.setTranType("REC");
		debitTransaction.setFund(receipt.getFund());
		debitTransaction.setTranTo(receipt.getCustomerId());
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType(receipt.getCustomerType());
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;
	}

	public List<MoneyTransaction> getRedemptionReversTranasactions(CustomerRedemptionRequest customerRedemptionRequest) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Credit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("C");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(customerRedemptionRequest.getTotAmount());
		creditTransaction.setTranTypeId(customerRedemptionRequest.getId());
		creditTransaction.setTranType("RED");
		creditTransaction.setTranTo(customerRedemptionRequest.getFund().getId());
		creditTransaction.setFund(customerRedemptionRequest.getFund());
		creditTransaction.setEffectDate(new Date());
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("FUN");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("D");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(new Date());
		debitTransaction.setTranAmont(customerRedemptionRequest.getTotAmount().negate());
		debitTransaction.setTranTypeId(customerRedemptionRequest.getId());
		debitTransaction.setTranType("REC");
		debitTransaction.setFund(customerRedemptionRequest.getFund());
		debitTransaction.setTranTo(customerRedemptionRequest.getCustomer().getId());
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType("CUS");
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;

	}

	public List<MoneyTransaction> getPaymentTransactionRev(Payment payment) {

		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();

		// Credit Transaction
		MoneyTransaction creditTransaction = new MoneyTransaction();
		creditTransaction.setCreditDebit("D");
		creditTransaction.setDate(new Date());
		creditTransaction.setTranAmont(payment.getAmount().negate());
		creditTransaction.setTranTypeId(payment.getId());
		creditTransaction.setTranType("PAY");
		creditTransaction.setTranTo(payment.getFund().getId());
		creditTransaction.setFund(payment.getFund());
		creditTransaction.setEffectDate(new Date());
		creditTransaction.setStatus("RC");
		creditTransaction.setCustomerType("FUN");
		creditTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(creditTransaction);

		// Credit Transaction
		MoneyTransaction debitTransaction = new MoneyTransaction();
		debitTransaction.setCreditDebit("C");
		debitTransaction.setDate(new Date());
		debitTransaction.setEffectDate(new Date());
		debitTransaction.setTranAmont(payment.getAmount());
		debitTransaction.setTranTypeId(payment.getId());
		debitTransaction.setTranType("PAY");
		debitTransaction.setFund(payment.getFund());
		debitTransaction.setTranTo(payment.getCustomerId());
		debitTransaction.setStatus("RC");
		debitTransaction.setCustomerType(payment.getCustomerType());
		debitTransaction.setUser(LoginAction.getCurrentLogingUser());

		moneyTransactions.add(debitTransaction);

		return moneyTransactions;
	}

	public List<MoneyTransaction> getJournalTransaction(JounalEntryDto jounalEntryDto, Journalentry journalentry) {
		List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();
		Fund fund = dao.getById(jounalEntryDto.getFundId());
		if (jounalEntryDto.getCreditDebit().equals("C")) {
			MoneyTransaction creditTransaction = new MoneyTransaction();
			creditTransaction.setCreditDebit("C");
			creditTransaction.setDate(new Date());
			creditTransaction.setTranAmont(journalentry.getAmount());
			creditTransaction.setTranTypeId(journalentry.getId());
			creditTransaction.setTranType("JOUR");
			creditTransaction.setTranTo(jounalEntryDto.getCustomerId());
			creditTransaction.setFund(fund);
			try {
				creditTransaction.setEffectDate(format.parse(jounalEntryDto.getJournalDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			creditTransaction.setStatus("RC");
			creditTransaction.setCustomerType(jounalEntryDto.getCustomerType());
			creditTransaction.setUser(LoginAction.getCurrentLogingUser());

			moneyTransactions.add(creditTransaction);

			// Credit Transaction
			MoneyTransaction debitTransaction = new MoneyTransaction();
			debitTransaction.setCreditDebit("D");
			debitTransaction.setDate(new Date());
			try {
				debitTransaction.setEffectDate(format.parse(jounalEntryDto.getJournalDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			debitTransaction.setTranAmont(journalentry.getAmount().negate());
			debitTransaction.setTranTypeId(journalentry.getId());
			debitTransaction.setTranType("JOUR");
			debitTransaction.setFund(fund);
			debitTransaction.setTranTo(fund.getId());
			debitTransaction.setStatus("RC");
			debitTransaction.setCustomerType("FUN");
			debitTransaction.setUser(LoginAction.getCurrentLogingUser());

			moneyTransactions.add(debitTransaction);
		} else {
			MoneyTransaction creditTransaction = new MoneyTransaction();
			creditTransaction.setCreditDebit("D");
			creditTransaction.setDate(new Date());
			creditTransaction.setTranAmont(journalentry.getAmount().negate());
			creditTransaction.setTranTypeId(journalentry.getId());
			creditTransaction.setTranType("JOUR");
			creditTransaction.setTranTo(jounalEntryDto.getCustomerId());
			creditTransaction.setFund(fund);
			try {
				creditTransaction.setEffectDate(format.parse(jounalEntryDto.getJournalDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			creditTransaction.setStatus("RC");
			creditTransaction.setCustomerType(jounalEntryDto.getCustomerType());
			creditTransaction.setUser(LoginAction.getCurrentLogingUser());

			moneyTransactions.add(creditTransaction);

			// Credit Transaction
			MoneyTransaction debitTransaction = new MoneyTransaction();
			debitTransaction.setCreditDebit("C");
			debitTransaction.setDate(new Date());
			try {
				debitTransaction.setEffectDate(format.parse(jounalEntryDto.getJournalDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			debitTransaction.setTranAmont(journalentry.getAmount());
			debitTransaction.setTranTypeId(journalentry.getId());
			debitTransaction.setTranType("JOUR");
			debitTransaction.setFund(fund);
			debitTransaction.setTranTo(fund.getId());
			debitTransaction.setStatus("RC");
			debitTransaction.setCustomerType("FUN");
			debitTransaction.setUser(LoginAction.getCurrentLogingUser());

			moneyTransactions.add(debitTransaction);

		}

		return moneyTransactions;
	}

}
